import sys
import urllib2
import HTMLParser

from BeautifulSoup import BeautifulSoup
from datiandouweb.models import Postion,Language
import json
import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "datiandou.settings")



all='http://buy.jd.com/purchase/flows/easybuy/FlowService.ashx?callback=jQuery1505790282269008458_1369722210529&action=GetAllAreas&IdProvince=-22&IdCity=-22&IdCounty=-22&_=1369722211049'

city='http://buy.jd.com/purchase/flows/easybuy/FlowService.ashx?callback=jQuery1506785497243981808_1369664534661&action=GetCitys&Id=-_-&_=1369665043550'
country='http://buy.jd.com/purchase/flows/easybuy/FlowService.ashx?callback=jQuery15001886157551780343_1369721717148&action=GetCountys&Id=-_-&_=1369721766942'
cookies='eb=0; _jzqx=1.1368179992.1368183148.2.jzqsr=item%2Ejd%2Ecom|jzqct=/258820%2Ehtml.jzqsr=item%2Ejd%2Ecom|jzqct=/1000097006%2Ehtml; _jzqa=1.4432381493364001300.1368179992.1368179992.1368183148.2; aview=1395.1000097006|1391.258820|1392.1012423498|1396.1012659185|1389.1005079224|1390.1016715352|1389.1014623505|1412.238198; atw=1395.1000097006.24|1392.1012423498.4|1391.258820.-5|1405.710699.-6|1396.1012659185.-21|1412.238198.-21|1390.1016715352.-34|1389.1005079224.-34; ipLocation=%u4E0A%u6D77; ipLoc-djd=2-0-0-0; _pst=luobintianya; pin=luobintianya; unick=%E9%AA%86%E5%AE%BE%E5%A4%A9%E6%B6%AF; ceshi3.com=E42248E18A6A4448E033B2F5331F056AD665DDC617F08768E13A882B7C8E44DCA5343C1F87C25E586DB0F67C1BE94AE731C23B882496DE03FE5678AE8FB7019926454607EA0F96DE313BAB85E3E4B27B595D01FF8DD5D11BB7A5BA77109787C6F92BC9273DFD28087CC445804040B42B46C9A8DB5296FD38E4586FBE01096B62E7789D3AA6527BB116EA3479AE58A167; ASP.NET_SessionId=prgwkyewutuigg45mmsnvp45; __jda=122270672.200675268.1368168753.1369643977.1369664493.5; __jdc=122270672; __jdv=122270672|direct|-|none|-; _jzqco=%7C%7C%7C%7C1369643980763%7C1.1578991340.1368168753574.1369664510443.1369664519933.1369664510443.1369664519933..0.0.226.226; yOrderOrder={&Pin&:null$&Name&:null$&IdProvince&:0$&IdCity&:0$&IdArea&:0$&IdTown&:0$&IsUseBalance&:false$&Price&:0$&OrderBulk&:0$&PromotionPrice&:0$&IdPaymentType&:4$&IdShipmentType&:70$&CODTime&:0$&ShipTime&:0$&PaymentWay&:0$&IdInvoicePutType&:0$&IdInvoiceType&:1$&IdInvoiceHeaderType&:4$&IdInvoiceContentsType&:1$&IdCompanyBranch&:0$&UserLevel&:0}6690E69D1EAB61D6; yCartOrder=; cn='
import time

for obj in Postion.objects.extra(where=["Postion_type='2'", "Postion_id >='51052'"]).order_by('Postion_id'):
    newcity =country.replace('-_-',str(obj.Postion_id))
    print(newcity)
    time.sleep(2)
    request = urllib2.Request(newcity)
    request.add_header('Cookie', cookies)
    page=urllib2.urlopen(request)
    soup=BeautifulSoup(page)
    print(soup)
    body = soup.text
    data= body.replace('jQuery15001886157551780343_1369721717148(','').replace(')','')
    print(data)
    jsondata= json.loads(data)['Obj']['Areas']

    for value in jsondata:
        id=str(value['Id'])
        name=value['Name'].encode("utf-8")
        print(id)
        print(name)
        position =Postion()
        position.Postion_id=id
        position.Postion_name=name
        position.Postion_type=3
        position.Remark=obj.Postion_id
        position.save()




